package com.heima.wemedia.controller.v1;

import com.heima.common.constants.wemedia.WemediaConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.media.dtos.NewsAuthDto;
import com.heima.model.media.dtos.WmNewsDto;
import com.heima.model.media.dtos.WmNewsPageReqDto;
import com.heima.model.media.pojos.WmNews;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController {
    @Autowired
    WmNewsService wmNewsService;

    @PostMapping("list")
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findAll(dto);
    }

    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDto wmNewsDto) {
        if (wmNewsDto.getStatus() == WmNews.Status.SUBMIT.getCode()) {
            //提交文章
            return wmNewsService.submitNews(wmNewsDto, WmNews.Status.SUBMIT.getCode());
        } else {
            //保存草稿
            return wmNewsService.submitNews(wmNewsDto, WmNews.Status.NORMAL.getCode());
        }
    }

    @GetMapping("one/{id}")
    public ResponseResult findWmNewsById(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsById(id);
    }

    @GetMapping("del_news/{id}")
    public ResponseResult delNewsById(@PathVariable("id") Integer id) {
        return wmNewsService.delNewsById(id);
    }

    @PostMapping("down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDto wmNewsDto) {
        return wmNewsService.downOrUp(wmNewsDto);
    }

    @GetMapping("/findOne/{id}")
    public WmNews findById(@PathVariable("id") Integer id) {
        return wmNewsService.getById(id);
    }

    @PostMapping("/update")
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult();
    }

    @GetMapping("/findRelease")
    public List<Integer> findRelease() {
        return wmNewsService.findRelease();
    }

    @PostMapping("/list_vo")
    public ResponseResult findList(@RequestBody NewsAuthDto newsAuthDto) {
        return wmNewsService.findList(newsAuthDto);
    }

    @GetMapping("/one_vo/{id}")
    public ResponseResult findWmNewsVoById(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsVoById(id);
    }

    @PostMapping("/auth_pass")
    public ResponseResult authPass(@RequestBody NewsAuthDto newsAuthDto) {
        return wmNewsService.updateStatus(newsAuthDto, WemediaConstants.WM_NEWS_AUTH_PASS);
    }

    @PostMapping("/auth_fail")
    public ResponseResult authFail(@RequestBody NewsAuthDto newsAuthDto) {
        return wmNewsService.updateStatus(newsAuthDto, WemediaConstants.WM_NEWS_AUTH_FAIL);
    }
}
